const GATEWAY_HOST = 'http://localhost:7001'
const UAA_SERVICE_NAME = '/ttxs-uaa'
const UAA_COMMON = GATEWAY_HOST + UAA_SERVICE_NAME + '/common'
const API_PREFIX = GATEWAY_HOST + '/app1'

const Global = {
    TOKEN_INFO: 'tokenInfo'
}
const Api = {
    login: {
        login: UAA_COMMON + '/login'
        ,register: UAA_COMMON + '/register'
        ,logout: UAA_COMMON + '/logout'
    }
    ,platform: GATEWAY_HOST + UAA_SERVICE_NAME + '/ttxs/platform'
    ,uaa: {
        menu: {
            menu: UAA_COMMON + '/menu'
            // 用户菜单树
            ,user: UAA_COMMON + '/menu/user'
            // 角色菜单
            //,role: UAA_COMMON + '/menu/role'
            ,refresh: UAA_COMMON + '/menu/refresh'
        }
        ,auth: UAA_COMMON + '/auth'
        ,role: UAA_COMMON + '/role'
        ,user: UAA_COMMON + '/user'
        ,resetPassword: UAA_COMMON + '/user/resetPassword'
        ,person: {
            changePassword: UAA_COMMON + '/person/changePassword'
        }
        ,token: {
            refresh: UAA_COMMON + '/token/refresh'
        }
    }
    // 非系统功能api需要添加平台名称前缀
    ,student: API_PREFIX + '/' + getTokenInfo().platformName + '/student'

}

function getTokenInfo() {
    return JSON.parse(sessionStorage.getItem(Global.TOKEN_INFO)) || {}
}

function ajaxSetup($) {
    $.ajaxSetup({
        headers:{
            'Authorization':  'Bearer ' + getTokenInfo().token
            ,'User-Id': getTokenInfo().userId
            ,'Platform-Id': getTokenInfo().platformId
            ,'Platform-Name': getTokenInfo().platformName
            ,'Role-Names': getTokenInfo().roleNames
        }
        ,xhrFields: {
            withCredentials: true
        }
    });
}

function getCountdown(totalSeconds) {
    let days = Math.floor(totalSeconds / (60 * 60 * 24));
    let modulo = totalSeconds % (60 * 60 * 24); //取模（余数）
    let hours = Math.floor(modulo / (60 * 60));
    modulo = modulo % (60 * 60);
    let minutes = Math.floor(modulo / 60);
    let seconds = modulo % 60;
    days = days.toString().length == 1 ? '0' + days : days;
    hours = hours.toString().length == 1 ? '0' + hours : hours;
    minutes = minutes.toString().length == 1 ? '0' + minutes : minutes;
    seconds = seconds.toString().length == 1 ? '0' + seconds : seconds;
    return days + "天" + hours + "时" + minutes + "分" + seconds + '秒'
}
